package com.example.contentProvider;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

/**
 * author:alexzhou email :zhoujiangbohai@163.com date :2013-2-25
 **/

public class NoteContentProvider extends ContentProvider {
	private static final UriMatcher sUriMatcher;
	private static final int COLLECTION_INDICATOR = 1;
	private static final int SINGLE_INDICATOR = 2;
	private DatabaseHelper mDbHelper;
	private static HashMap<String, String> sNotesProjectionMap;
	static {
		sNotesProjectionMap = new HashMap<String, String>();
		sNotesProjectionMap.put(NoteProviderMetaData.NoteTableMetaData._ID,
				NoteProviderMetaData.NoteTableMetaData._ID);
		sNotesProjectionMap.put(
				NoteProviderMetaData.NoteTableMetaData.NOTE_CONTENT,
				NoteProviderMetaData.NoteTableMetaData.NOTE_CONTENT);
		sNotesProjectionMap.put(
				NoteProviderMetaData.NoteTableMetaData.NOTE_TITLE,
				NoteProviderMetaData.NoteTableMetaData.NOTE_TITLE);
		sNotesProjectionMap.put(
				NoteProviderMetaData.NoteTableMetaData.CREATE_DATE,
				NoteProviderMetaData.NoteTableMetaData.CREATE_DATE);
	}
	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(NoteProviderMetaData.AUTHORITY, "notes",
				COLLECTION_INDICATOR);
		sUriMatcher.addURI(NoteProviderMetaData.AUTHORITY, "notes/#",
				SINGLE_INDICATOR);
	}

	@Override
	public boolean onCreate() {
		mDbHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		switch (sUriMatcher.match(uri)) {
		case COLLECTION_INDICATOR:

			// 设置查询的表
			queryBuilder
					.setTables(NoteProviderMetaData.NoteTableMetaData.TABLE_NAME);

			// 设置投影映射
			queryBuilder.setProjectionMap(sNotesProjectionMap);
			break;

		case SINGLE_INDICATOR:
			queryBuilder
					.setTables(NoteProviderMetaData.NoteTableMetaData.TABLE_NAME);
			queryBuilder.setProjectionMap(sNotesProjectionMap);
			queryBuilder.appendWhere(NoteProviderMetaData.NoteTableMetaData._ID
					+ "=" + uri.getPathSegments().get(1));
			break;

		default:
			throw new IllegalArgumentException("Unknow URI: " + uri);
		}

		String orderBy;
		if (TextUtils.isEmpty(sortOrder)) {
			orderBy = NoteProviderMetaData.NoteTableMetaData.DEFAULT_ORDERBY;
		} else {
			orderBy = sortOrder;
		}
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor cursor = queryBuilder.query(db, projection, selection,
				selectionArgs, null, null, orderBy);

		return cursor;
		// TODO Auto-generated method stub
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
		case COLLECTION_INDICATOR:
			return NoteProviderMetaData.NoteTableMetaData.CONTENT_TYPE;

		case SINGLE_INDICATOR:
			return NoteProviderMetaData.NoteTableMetaData.CONTENT_ITEM_TYPE;

		default:
			throw new IllegalArgumentException("Unknow URI: " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		if (sUriMatcher.match(uri) != COLLECTION_INDICATOR) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long rowID = db
				.insert(NoteProviderMetaData.NoteTableMetaData.TABLE_NAME,
						null, values);

		if (rowID > 0) {
			Uri retUri = ContentUris.withAppendedId(
					NoteProviderMetaData.NoteTableMetaData.CONTENT_URI, rowID);
			return retUri;
		}

		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = -1;
		switch (sUriMatcher.match(uri)) {
		case COLLECTION_INDICATOR:
			count = db.delete(
					NoteProviderMetaData.NoteTableMetaData.TABLE_NAME,
					selection, selectionArgs);
			break;

		case SINGLE_INDICATOR:
			String rowID = uri.getPathSegments().get(1);
			count = db.delete(
					NoteProviderMetaData.NoteTableMetaData.TABLE_NAME,
					NoteProviderMetaData.NoteTableMetaData._ID + "=" + rowID,
					null);
			break;

		default:
			throw new IllegalArgumentException("Unknow URI :" + uri);

		}

		// 更新数据时，通知其他ContentObserver
		this.getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = -1;
		switch (sUriMatcher.match(uri)) {
		case COLLECTION_INDICATOR:
			count = db.update(
					NoteProviderMetaData.NoteTableMetaData.TABLE_NAME, values,
					null, null);
			break;

		case SINGLE_INDICATOR:
			String rowID = uri.getPathSegments().get(1);
			count = db.update(
					NoteProviderMetaData.NoteTableMetaData.TABLE_NAME, values,
					NoteProviderMetaData.NoteTableMetaData._ID + "=" + rowID,
					null);
			break;

		default:
			throw new IllegalArgumentException("Unknow URI : " + uri);

		}
		this.getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}